<?php

namespace slinks\extensions\action\compiler;

use slinks\core\DependencyInjection\Reference;
use slinks\core\DependencyInjection\Definition;
use slinks\core\dependencyInjectionExt\compiler\AbstractInterfaceProcessingPass;
use slinks\core\DependencyInjection\ContainerBuilder;
use slinks\core\DependencyInjection\Compiler\CompilerPassInterface;
use slinks\extensions\controller\ControllerExtension;

class ControllerPass extends AbstractInterfaceProcessingPass {

    private $delegatingControllerDef = null;

    const CONTROLLER_ID = 'slinks.front.controller';
    
    function __construct() {
        parent::__construct('slinks\\extensions\\action\\core\\IFrontController');
    }

    /**
     * Allows sublcasses to check any pre-requisites prior to processing individual definitions. 
     * 
     * @param ContainerBuilder $container
     * @return Boolean true if the definition processing should occur. 
     */
    public function shouldProcessBegin(ContainerBuilder $container) {
        // we must make sure the twig extension is configured and the twig classes are available.
        $ret = $container->hasDefinition(self::CONTROLLER_ID);

        if ($ret) {
            $this->delegatingControllerDef = $container->getDefinition(self::CONTROLLER_ID);
        }
        return $ret;
    }

    /**
     * You can modify the Definition here before it is dumped to PHP code.
     * 
     * @param string $id the service definition id; 
     * @param Definition $definition
     */
    public function processDefinition($id, Definition $definition) {
        // make sure not self
        if($id != self::CONTROLLER_ID){
            $this->delegatingControllerDef->addMethodCall('addFrontController', array(new Reference($id)));
        }
    }

}